// pages/W_Index/index.js
var CommentUtils = getApp().CommonUtils;
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
	data: {
		userInfo: {},
		hasUserInfo: true,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		one: "",
		two: "",
		three: "",
		four: "",
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		if (options.scene) {
			var scene = decodeURIComponent(options.scene)
			console.log('scene', scene)
		}

		this.requestNetGetJSPIOpenID();
		this.requestNetHomeAD();
		this.requestNetHomeButton();
		this.requestNetHomeProductList();
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		this.requestNetHomeProductList();
		var _this = this;

		 var value = wx.getStorageSync('HasUserInfo');
		 if (value != true) {
       _this.setData({
         hasUserInfo:false
       })
     }else{
       _this.setData({
         hasUserInfo:true
       })
     }

/*
			if (app.globalData.userInfo!=null) {
				_this.setData({
					hasUserInfo: true
				})
				_this.data.hasUserInfo = true;
			} else if (_this.data.canIUse) {
				_this.setData({
					hasUserInfo: false
				})
				// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
				// 所以此处加入 callback 以防止这种情况
				app.userInfoReadyCallback = res => {
					if (res.userInfo) {
            console.log("1111111111111111111")
            console.log(res.userInfo)
            _this.requestNetUInfoInsert(
              res.userInfo.country,
              res.userInfo.province,
              res.userInfo.city,
              res.userInfo.avatarUrl,
              res.userInfo.nickName,
              res.userInfo.gender)
						_this.setData({
							hasUserInfo: true
						})
					}
					else {
						_this.setData({
							hasUserInfo: false
						})
					}
				}
			}

      */
		// }else{
    //   console.log("ssdddzz");
    //   console.log(value)
    //   _this.setData({
    //     hasUserInfo: true
    //   })
    // }
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () {

	},

	/**
	 * 网络请求--用户登录并获取Token
	 */
	requestNetGetJSPIOpenID() {
		wx.login({
			success: res => {
				if (res.code) {
					wx.request({
						url: 'https://www.chnode.xyz/api/Other.ashx?DataType=GetJSPIOpenID&js_code=' + res.code,
						success: function (e) {
              console.log("aaaaaa");
              console.log(e);
              console.log("aaaaaa");
							if (e.data.isok == 'true') {
								getApp().data.openid = e.data.data.openid
								getApp().data.Token = e.data.data.Token
							} else {
								wx.showModal({
									title: '提示',
									content: e.data.info,
									showCancel: false,
								})
							}
						},
						fail: function (e) {
						}
					})
				}
			}
		})
	},

	/**
	 * 网络请求--首页轮播
	 */
	requestNetHomeAD: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_AD',
			success: function (res) {

				if (res.data.isok == "true") {
					_this.setData({
						HomeADs: res.data.datas
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
	 * 网络请求--首页导航，四个按钮
	 */
	requestNetHomeButton: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_Button',
			success: function (res) {
				if (res.data.isok == "true") {
					_this.setData({
						HomeButtons: res.data.datas,
						one: res.data.datas[0].SAS_Link,
						two: res.data.datas[1].SAS_Link,
						three: res.data.datas[2].SAS_Link,
						four: res.data.datas[3].SAS_Link,
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
	 * 网络请求--商品列表
	 */
	requestNetHomeProductList: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_ProductList',
			data: {
				AS_ID: getApp().data.AS_ID,
				A_ID: getApp().data.A_ID,
			},
			success: function (res) {
				console.log(res.data.datas)
				if (res.data.isok == "true") {
					_this.setData({
						HomeProductList: res.data.datas
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},



	/**
	 * 网络请求--搜索
	 */
	requestNetHomeSearch: function (res) {
		var _this = this;
		var value = res.detail.value;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_Search',
			data: {
				Search: value,
        AgentSaleID: getApp().data.AS_ID,
        AgentID: getApp().data.A_ID,
			},
			success: function (res) {
				wx.hideNavigationBarLoading()
				if (res.data.isok == 'true' && res.data.datas.length > 0) {
					wx.navigateTo({
						url: '../Fl_Search/index?key=' + value + '&datas=' + JSON.stringify(res.data.datas),
					})
				} else {
					CommentUtils.showToastWithImg("未找到商品", 3)
				}
			},
			fail: function (res) {
				wx.hideNavigationBarLoading()
				CommentUtils.showToastWithImg("请稍后尝试", 3)
			},
			complete: function (res) { wx.hideNavigationBarLoading() }
		})
	},

	/**
	 * 网络请求--绑定用户信息
	 */
	requestNetUInfoInsert: function (param1, param2, param3, param4, param5, param6) {
		var _this = this;
		wx.request({
			url: 'https://www.chnode.xyz/api/My.ashx?DataType=UInfoInsert',
			data: {
				Openid: getApp().data.openid,
				Country: param1,
				Privince: param2,
				City: param3,
				HeadimgUrl: param4,
				NickName: param5,
				Sex: param6,
			},
			success: function (res) {
        wx.setStorageSync("HasUserInfo", true)
        _this.setData({
          hasUserInfo: true
        })
			}
		})
	},

	/**
	 * 点击事件--首页导航按钮
	 */
	btnEventHomeButton: function (res) {
		var index = res.currentTarget.dataset.index;
		var pageList = ['Fl_PinPai', 'Fl_FuWu', 'Fl_ZhaoShang', 'Fl_GouMai'];

		var _this = this;
		var padeLink = [_this.data.one, _this.data.two, _this.data.three, _this.data.four];

		wx.navigateTo({
			url: '../' + pageList[index] + '/index?id=' + padeLink[index],
		})
	},

	/**
	 * 点击事件--跳转商品详情页
	 */
	btnEventHomeProductList: function (res) {
		wx.navigateTo({
			url: "/pages/W_ProductDetail/index?pid=" + res.currentTarget.dataset.p_id
		})
	},

	/**
	 * 点击事件--获取个人信息--取消
	 */
	btnEventUserInfoCancel: function (res) {
		this.setData({
			hasUserInfo: false
		})
	},

	/**
	 * 点击事件--获取个人信息--确认
	 */
	btnEventUserInfoGet: function (e) {
		if (e.detail.userInfo) {
			app.globalData.userInfo = e.detail.userInfo
			this.requestNetUInfoInsert(
				e.detail.userInfo.country,
				e.detail.userInfo.province,
				e.detail.userInfo.city,
				e.detail.userInfo.avatarUrl,
				e.detail.userInfo.nickName,
				e.detail.userInfo.gender)
			
		}
	},
	/**
	 * 点击事件--扫描二维码
	 */
	btnEventScan: function (res) {
		var _this = this;
		wx.scanCode({
			onlyFromCamera: false,
			scanType: 'qrCode',
			success: function (res) {
				var result = JSON.parse(res.result)
				if (result.Type == '促销活动') {
					if (result.AS_ID != '' && result.A_ID != '') {
						getApp().data.AS_ID = result.AS_ID
						getApp().data.A_ID = result.A_ID
						_this.requestNetHomeProductList();
					}
				} else if (result.Type == '充值卡') {
           console.log("充值卡");
          if (result.R_Key != '' && result.R_Code != '' && result.R_Money != ''){
            var str = JSON.stringify({ "R_Code": result.R_Code, "R_Key": result.R_Key, "R_Money": result.R_Money });
            wx.navigateTo({
              url: '../W_ChongZhi/index?str=' + str,
            })
          }

				}
			}
		})
	},
})